{% extends 'base/base_table.html' %}

{% block title %}ITAdmin | 终端管理{% endblock %}

{% block css %}
    <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
{% endblock %}

{% block table_thead %}
<tr>
    <th style="text-align: center;vertical-align: middle">用户名</th>
    <th style="text-align: center;vertical-align: middle">可连接主机</th>
    <th style="text-align: center;vertical-align: middle">操作</th>
</tr>
{% endblock %}

{% block table_btn %}
    <button class="btn btn-sm btn-success" id="adduser" href="{% url 'addterminaluser' %}"><strong>添加连接用户</strong></button>
{% endblock %}

{% block table_bady %}
 {% for object in objects %}
    <tr>
        <td style="text-align: center;vertical-align: middle">{{ object.user.username }}</td>
        <td style="text-align: center;vertical-align: middle">{{ object.get_servers }}</td>
        <td style="text-align: center;vertical-align: middle">
            <a class="btn btn-sm btn-danger" id="del_{{ object.user.id }}" name="{{ object.user.id }}">
                删除<i class="fa fa-bitbucket"></i>
            </a>
            <a class="btn btn-sm btn-primary" id="edit_{{ object.user.id }}" name="{{ object.user.id }}" href="{% url 'addterminaluser' %}?user_id={{ object.user.id }}">
                编辑<i class="fa fa-pencil"></i>
            </a>
        </td>
    </tr>
 {% endfor %}
{% endblock %}

{% block base_script %}
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
{% endblock %}

{% block script %}
<script type="text/javascript">

    $(function () {
        $('#pages').val('{{ entry }}')

        $("a[id^='del_']").click(function () {
            var user_id = $(this).attr('name')
            swal({
                title: "确定删除?",
                text: "您是否确定要删除该连接用户!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
                }, function () {
                $.post("{% url 'del_terminal_user' %}", {user_id: user_id}, function (data) {
                    if (data.status == 1) {
                        swal({title:"成功!", text:"删除用户成功", type:"success"}, function () {
                            location.href = "{% url 'terminals' %}";
                        });
                    } else {
                        swal({title:"错误!", text:data.msg, type:"error"}, function () {
                             location.href = "{% url 'terminals' %}";
                        });
                    }
                }, 'json');
            });
        });

        $('#pages').change(function () {
            var search = '{{ search }}'
            if (search == '') {
                location.href = "{% url 'terminals' %}?page={{ num }}&entry=" + $(this).val();
            } else {
                location.href = "{% url 'terminals' %}?page={{ num }}&{{ search }}&entry=" + $(this).val();
            }
        });

        $('#adduser').click(function () {
            location.href = "{% url 'addterminaluser' %}";
        })
    });
</script>
{% endblock %}